package com.samehope.ar.controller;


import com.samehope.ar.common.CommonResult;
import com.samehope.ar.dto.PatrolPointCountParam;
import com.samehope.ar.dto.PointAddParam;
import com.samehope.ar.dto.PointUpdateParam;
import com.samehope.ar.service.IPatrolPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 巡检点 前端控制器
 * </p>
 *
 * @author Zhang Luo
 * @since 2019-12-23
 */
@RestController
@RequestMapping("/point")
public class PatrolPointController {

    @Autowired
    private IPatrolPointService pointService;

    /**
     * 新增巡检点
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @PostMapping("/add")
    public CommonResult add(@Valid @RequestBody PointAddParam param) {
        return CommonResult.success(pointService.add(param));
    }

    /**
     * 更新巡检点
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @PutMapping("/update")
    public CommonResult update(@Valid @RequestBody PointUpdateParam param) {
        pointService.update(param);
        return CommonResult.success(null);
    }

    /**
     * 删除巡检点
     * @param pointId
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @DeleteMapping("/delete/{pointId}")
    public CommonResult delete(@PathVariable("pointId") Long pointId) {
        pointService.deletePoint(pointId);
        return CommonResult.success(null);
    }

    /**
     * 获取巡检点明细
     * @param pointId
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @GetMapping("/detail/{pointId}")
    public CommonResult detail(@PathVariable("pointId") Long pointId) {
        return CommonResult.success(pointService.detail(pointId));
    }

    /**
     * 获取巡检点树结构
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @GetMapping("/tree")
    public CommonResult tree(@RequestParam(required = false) String condition) {
        return CommonResult.success(pointService.getPointTree(condition));
    }

    /**
     * 获取巡检点树结构, 没有点位的分类不显示
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @GetMapping("/treeWithFullNode")
    public CommonResult treeWithFullNode() {
        return CommonResult.success(pointService.getPointTreeWithFullNode());
    }

    /**
     * 统计巡检点历史
     * @param param
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @GetMapping("/count")
    public CommonResult count(@Valid PatrolPointCountParam param){
        return CommonResult.success(pointService.countByPoint(param));
    }

    /**
     * 统计巡检点问题类型分布
     * @param param
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @GetMapping("/countByProblemType")
    public CommonResult countByProblemType(@Valid PatrolPointCountParam param){
        return CommonResult.success(pointService.countByProblemType(param));
    }

    /**
     * 统计巡检点问题中检查项分布
     * @param param
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @GetMapping("/countByProblemItem")
    public CommonResult countByProblemItem(@Valid PatrolPointCountParam param){
        return CommonResult.success(pointService.countByProblemItem(param));
    }
}
